package models

import (
	"time"
)

type ApiTokenLog struct {
	ID        uint      `gorm:"primaryKey" json:"id"`
	UserID    uint      `gorm:"not null;index" json:"user_id"`
	Action    string    `gorm:"not null;size:50" json:"action"` // generated, rotated, revoked
	TokenHash string    `gorm:"not null;size:255" json:"token_hash"`
	IPAddress string    `gorm:"size:45" json:"ip_address"`
	UserAgent string    `gorm:"type:text" json:"user_agent"`
	Notes     *string   `gorm:"type:text" json:"notes,omitempty"`
	CreatedAt time.Time `json:"created_at"`

	// 关联
	User User `gorm:"foreignKey:UserID" json:"user"`
}

// Log 创建日志记录
func Log(userID uint, action, tokenHash, ipAddress, userAgent string, notes *string) *ApiTokenLog {
	return &ApiTokenLog{
		UserID:    userID,
		Action:    action,
		TokenHash: tokenHash,
		IPAddress: ipAddress,
		UserAgent: userAgent,
		Notes:     notes,
		CreatedAt: time.Now(),
	}
}